package org.project.controller2;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.annotations.Param;
import org.project.pojo.Admintime;
import org.project.pojo.CityAddress0;
import org.project.pojo.Doreport;
import org.project.pojo.Fit;
import org.project.pojo.Ordertime1;
import org.project.pojo.Ordertime2;
import org.project.pojo.Remaintimes;
import org.project.pojo.Times;
import org.project.service.user.Order1Service;
import org.project.service.user.OrdertimeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/Ordertimecontroller")
public class Ordertimecontroller {

	
	@Resource
	private OrdertimeService ordertimeService;
	
	@Resource
	private Order1Service order1Service;
	
	@RequestMapping("/showtimes")
	@ResponseBody
	public  List<String> showtimes(@Param("shopid")String shopid,@Param("fitnum")String fitnum,@Param("date")String date){
	 //查询剩余时间表（remaintimes）是否有当天的数据   
		List<String> list=selectremaintimes(Integer.valueOf(shopid),date);
		
		if(list.size()==0){//剩余时间表里没有当天此店铺的信息
			Times times=new Times();
			times.setId(Integer.valueOf(shopid));
			Times time0=ordertimeService.selectalltime(times);
			String ordertimes=time0.getOrdertimes(); 
			  String arr[]=ordertimes.split(",");
			  for(int i=0;i<arr.length;i++){
				  String time=arr[i];
				  Remaintimes remaintimes=new Remaintimes(0,Integer.valueOf(shopid),date,time);
				  ordertimeService.insertremaintimes(remaintimes);
			  }//剩余时间表中已插入完整时间信息
			  
			   
			  
		}
		
		
		
	//在剩余时间内计算可预约时间（把每个剩余时间拿出来便利一下，加上所需时间后这段范围之内是否有  预约时间 、店铺结束时间、操作时间，有就remove）
		Remaintimes remaintimes0=new Remaintimes(0,Integer.valueOf(shopid),date,null);
		List<String> remaintimelist=ordertimeService.selectremaintimes(remaintimes0);//查询当天所有的剩余时间
		
		Iterator<String>it=remaintimelist.iterator();
		
	
		while(it.hasNext()){
			String starttime0=it.next();
			SimpleDateFormat sim=new SimpleDateFormat("HH:mm");
			Date starttime=null;
			try {
				starttime = sim.parse(starttime0);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}//剩余时间中的每个元素
			Date endtime=countendtime(starttime0,Integer.valueOf(fitnum)); //对应的结束时间
			
			//得到商店对应的结束时间
			CityAddress0 CityAddress0=new CityAddress0();
			CityAddress0.setId(Integer.valueOf(shopid));
			CityAddress0 CityAddress1=ordertimeService.selectendtime(CityAddress0);
			Date shopendtime=null;
			try {
				shopendtime = sim.parse(CityAddress1.getEndTime());
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			//判断是否在店铺结束时间之前
			if(endtime.after(shopendtime)){
				it.remove();
				continue;
			}
			
			
			//查询操作表里所有的该天的操作时间
			Doreport doreport0=new Doreport(0,Integer.valueOf(shopid),date,null,null);
			List<Doreport> doreportlist =ordertimeService.selectdoreport(doreport0);
			Iterator<Doreport> it2=doreportlist.iterator();
			
			while(it2.hasNext()){
				Doreport Doreport=it2.next();
				//System.out.println(Doreport);
				Date dotime=null;
				try {
					dotime = sim.parse(Doreport.getDotime());
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				System.out.println(dotime);
				
				if(null!=dotime){
				if((dotime.before(endtime)&&dotime.after(starttime))||(dotime.equals(starttime))){

					it.remove();
					break;
					
				}
					
				}
				
				
				
				
			}
			
			
			
			
			
		}
		            return remaintimelist;
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 	* @方法名:管理员显示剩余时间
	 * 	* @参数:shopid ,date
	 * 	* @输出:
	 * 	* @备注:
	 * 	* @作者: 钱程浩
	 * 	* @时间: 2018年2月1 11:38:31
	 * 	* @修改:
	 * 	*/
    @RequestMapping("/adminselremaintime")
    @ResponseBody
	public List<String> selremaintime(@Param("shopid")String shopid,@Param("date")String date){
    	 //查询剩余时间表（remaintimes）是否有当天的数据   
		List<String> list=selectremaintimes(Integer.valueOf(shopid),date);
		
		if(list.size()==0){//剩余时间表里没有当天此店铺的信息
			Times times=new Times();
			times.setId(Integer.valueOf(shopid));
			Times time0=ordertimeService.selectalltime(times);
			String ordertimes=time0.getOrdertimes(); 
			  String arr[]=ordertimes.split(",");
			  for(int i=0;i<arr.length;i++){
				  String time=arr[i];
				  Remaintimes remaintimes=new Remaintimes(0,Integer.valueOf(shopid),date,time);
				  ordertimeService.insertremaintimes(remaintimes);
			  }//剩余时间表中已插入完整时间信息
			  
		}
    	
    	
		
		
		
		
		Remaintimes remaintimes=new Remaintimes(0,Integer.valueOf(shopid),date,null);
		List<Ordertime2> list5=counttimelist(date,Integer.valueOf(shopid));//当天已预约的 所有  时间段
		
		
		
		
		List<String> list2=ordertimeService.adminremain(remaintimes);//当天所有剩余时间
		
		Iterator<String>it=list2.iterator();
		while(it.hasNext()){//便利当天所有的剩余时间(已过滤掉 被后台屏蔽的时间)  只需再排除当天 已预约的时间段
			
			String time=it.next();
			SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
			Date time2=null;      //把集合中的元素转换成日期格式
			try {
				time2=sdf.parse(time);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			 
			
			   Iterator<Ordertime2> it3=list5.iterator();
			   while(it3.hasNext()){   //便利每个时间段
				   Ordertime2 Ordertime2=it3.next();
				   if((time2.after(Ordertime2.getOrdertime())&&time2.before(Ordertime2.getEndtime()))||time2.equals(Ordertime2.getOrdertime())){
					  
					   
					   it.remove();
					     
					   
					   
				   }
				   
				   
			   }
			    
			
			
			
		}
		return list2;
	
    }
	
	
	
    
    
    
    
	//通过日期，店铺编号  返回该天该 店铺   所有已预约项目    的    日期，开始时间，结束时间 （后台使用）
    
	public List<Ordertime2> counttimelist(String date ,int shopid){
		
		 List<Ordertime2> Ordertime2list=new ArrayList<Ordertime2>(); //新建Ordertime2list
		
		 
		 Ordertime1 ordertime1=new 	Ordertime1();
		ordertime1.setShopid(shopid);
		ordertime1.setOrderdate(date);
		
		List<Admintime>list1 =order1Service.selectorderTime(ordertime1);
		Iterator<Admintime>it=list1.iterator();
		while(it.hasNext()){
			Admintime Admintime=it.next();
			String orderTime=Admintime.getOrderTime();
			int fitnum= Admintime.getFitnum();
			
			String arr[]=orderTime.split(" ");
			String starttime0=arr[1];
			SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
			Date starttime=null;
			try {
				starttime=sdf.parse(starttime0);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Date endtime=countendtime(starttime0,fitnum);
			
			Ordertime2 Ordertime2=new Ordertime2(date,starttime,endtime);
			
			Ordertime2list.add(Ordertime2);
			
		}
		   return Ordertime2list;
	}
	
	
	
	
	//通过适合人数查询需要的时间
	public int requiredtime(int fitnum){
		Fit fit0=new Fit(0,fitnum,0);
		Fit fit=ordertimeService.requiredtime(fit0);
		int requiredtime=fit.getRequiredtime();
		
		return requiredtime;
	}
	
	
	
	
	
	/**
	 * 	* @方法名:计算该类型项目的结束时间
	 * 	* @参数:开始时间、项目类型
	 * 	* @输出:
	 * 	* @备注:
	 * 	* @作者: 钱程浩
	 * 	* @时间: 2018年1月29 6:42:23
	 * 	* @修改:
	 * 	*/
	public  Date countendtime(String starttime,int fitnum) {
		Fit fit0=new Fit(0,fitnum,0);
		Fit fit=ordertimeService.requiredtime(fit0);
		int requiredtime=fit.getRequiredtime();
		
		//计算结束时间
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		 Date time=null;
		try {
			time = sdf.parse(starttime);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 Calendar cal = Calendar.getInstance();
		 cal.setTime(time);
		 cal.add(Calendar.MINUTE, +requiredtime);
		  time = cal.getTime();
		  
		 return time;
		 
	}


	
			//通过shopid查询所有原来时间
			public String selectalltime(int shopid){
				Times times=new Times(shopid,null);
				Times times2=ordertimeService.selectalltime(times);
				String ordertimes=times2.getOrdertimes();
				
				return ordertimes;
				
			}
		
	
	
	
	
			//通过shopid，详细日期  查询剩余时间
			public List<String> selectremaintimes(int shopid,String date){
			   
				Remaintimes remaintimes1=new Remaintimes(0,shopid,date,null);
				List<String> remaintimeslist=ordertimeService.selectremaintimes(remaintimes1);
			  
			    	
				return remaintimeslist;
				
			}
	

	
	
	
	//集合转时间字符串
	public static String listtostring(List<String> list){
		String times0="";
		Iterator<String> it=list.iterator();
		while(it.hasNext()){
			
			String time=it.next();
			times0=times0+time+",";
		}
		
		String times=times0.substring(0,times0.length()-1);
		return times;
	}
	
	
	
	
	
	
	
	//时间字符串转集合
	public static List<String> stringtolist(String times){
		List<String> timeslist=new ArrayList<String>();
		
		String arr[]=times.split(",");
		for(int i=0;i<arr.length;i++){
			timeslist.add(arr[i]);
			
		}
		
		return timeslist;
		
	}
	
	
	
	
	
	
	
	
	
	
/*	public static void main(String[] args) throws Exception {
		
		String arr[]={"9:30","10:00","10:30","11:00","11:30","12:00"};
		List<String> list2=new ArrayList<String>();
		for(int i=0;i<arr.length;i++){
			
			list2.add(arr[i]);
		}
		
		System.out.println(listtostring(list2));
		
		
		//System.out.println(stringtolist("9:30,10:00,10:30,11:00,11:30,12:00"));
		Date d=endtime("22:59",48);
	    System.out.println(new SimpleDateFormat("HH:mm").format(d));
		
		
	}*/
	
	
	
}
